#include "book.h"
#include "bookpage.h"

Book::Book(QObject *parent) :
    QObject(parent)
{

}

Book::~Book()
{
    m_pages.clear();
}

int Book::count() const
{
    return m_pages.count();
}

void Book::setFilenameList(QStringList& list)
{
    m_pages.clear();
    int count = 0;
    foreach(QString name, list){
        BookPage* page = new BookPage(this);
        page->setPage(name);
        page->setNumber(count);
        m_pages.append(page);
        count++;
    }

    emit imageFilesReady();
}

void Book::setBookName(QString name)
{
    m_bookname =name;
}

void Book::setBookArchiveName(QString name)
{
    m_archiveName = name;
}

QString Book::getBookArchiveName()
{
    return m_archiveName;
}

QString Book::getBookName()
{
    return m_bookname;
}

void Book::setListImage(QUrl img)
{
    m_img = img;
}

QUrl Book::getListImage()
{
    return m_img;
}

QDeclarativeListProperty<BookPage> Book::pages()
{
    return QDeclarativeListProperty<BookPage>(this,0,NULL,&Book::countFunction,&Book::atFunction,NULL);
}

BookPage* Book::atFunction(QDeclarativeListProperty<BookPage> *property, int index)
{
    Book* list = qobject_cast<Book*>(property->object);
    if(list){
        return list->m_pages.at(index);
    }else{
        return NULL;
    }
}

int Book::countFunction(QDeclarativeListProperty<BookPage> *property)
{
    Book* list = qobject_cast<Book*>(property->object);
    if(list){
        return list->m_pages.count();
    }else{
        return 0;
    }
}

Book::ArchiveType Book::getArchiveType()
{
    Book::ArchiveType ret = Book::Nothing;

    if(m_archiveName.endsWith(".zip",Qt::CaseInsensitive))
        ret = Book::Zip;
    if(m_archiveName.endsWith(".cbz",Qt::CaseInsensitive))
        ret = Book::Zip;
    if(m_archiveName.endsWith(".rar",Qt::CaseInsensitive))
        ret = Book::Rar;
    if(m_archiveName.endsWith(".cbr",Qt::CaseInsensitive))
        ret = Book::Rar;
    return ret;
}

void Book::setFirstPageFilename(QString name)
{
    m_firstPageFilename = name;
}

QString Book::getFirstPageFilename()
{
    return m_firstPageFilename;
}
